<?php

/**
 * 重新抓取核心品类商品(每七天更新一次，定义为每周一凌晨1:00开始)
 *
 **/


ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\wiki\Wiki as mWiki;
use Biz\model\wiki\WikiRankingCateHotUp as mWikiRankingCateHotUp;
use Biz\model\wiki\WikiRankingCatePublic as mWikiRankingCatePublic;
use Biz\model\wiki\WikiRankingHot as mWikiRankingHot;
use Biz\model\wiki\WikiRankingPublicPraise as mWikiRankingPublicPraise;
use Biz\scraper\GoodsInfo;
use Biz\service\wiki\AdmWiki;
use Biz\service\wikiRanking\Ranking;
use Doraemon\model\Category as mCategory;
use Doraemon\model\Es\WikiBasic as esWikiBasic;

//$isAll = !empty($argv[1]) ? $argv[1] : 0;
$isAll = 0;

$clickByNumTopWikiIds = mWiki::selectByWhere(['status[!]' => mWiki::STATUS_DEL, 'LIMIT' => [0, 2000], 'ORDER' => ['click_buy_num' => 'DESC']], 'id');

$rankingBasicIds = [];
$rankingDay = Ranking::getRankingDay();
$where = [
    'ranking_state' => mWikiRankingHot::RANKING_STATE_SHOW,
    'ranking_day' => $rankingDay
];
$hotBasicIds = mWikiRankingHot::selectByWhere($where, 'basic_id');
$hotBasicIds = array_unique($hotBasicIds);
$rankingBasicIds = array_merge($rankingBasicIds, $hotBasicIds);

$publicPraiseBasicIds = mWikiRankingPublicPraise::selectByWhere($where, 'basic_id');
$publicPraiseBasicIds = array_unique($publicPraiseBasicIds);
$rankingBasicIds = array_merge($rankingBasicIds, $publicPraiseBasicIds);

$cateHotUpBasicIds = mWikiRankingCateHotUp::selectByWhere($where, 'basic_id');
$cateHotUpBasicIds = array_unique($cateHotUpBasicIds);
$rankingBasicIds = array_merge($rankingBasicIds, $cateHotUpBasicIds);

$catePublicBasicIds = mWikiRankingCatePublic::selectByWhere($where, 'basic_id');
$catePublicBasicIds = array_unique($catePublicBasicIds);
$rankingBasicIds = array_merge($rankingBasicIds, $catePublicBasicIds);

var_dump('去购买top2000商品数量：' . count($clickByNumTopWikiIds), '上榜wiki数量：' . count($rankingBasicIds));

$categoryWikiIds = [];
//排除智能家居、宠物用品
$firstCategoryIds = mCategory::selectByWhere(['father_id' => 0, 'deep' => 1, 'status' => mCategory::STATUS_USING, 'id[!]' => [115, 120]], 'id');
$secondCategoryIds = mCategory::selectByWhere(['father_id' => $firstCategoryIds, 'deep' => 2, 'status' => mCategory::STATUS_USING], ['id', 'cate_name']);
foreach ($secondCategoryIds as $categoryRow) {
    $res = esWikiBasic::searchHotWikiBySecondCategory($categoryRow['id'], 0, 1000, 5);
    if ($res && $res['rows']) {
        $wikiIds = array_column($res['rows'], 'wiki_id');
        $wikiIds = array_filter(array_unique($wikiIds));
        var_dump('分类id#' . $categoryRow['id'] . '#' . $categoryRow['cate_name'] . '#下商品热度高于5分的商品数:' . count($wikiIds));
        $categoryWikiIds = array_merge($categoryWikiIds, $wikiIds);
    }
}
$categoryWikiIds = array_unique($categoryWikiIds);
var_dump('分类下商品热度高于5分的商品总数：' . count($categoryWikiIds));

//$taobaoCount = 0;
//$jdCount = 0;
//
//$list = [];
//$rankingWikiList = mWiki::selectByWhere(['basic_id' => $rankingBasicIds, 'is_default' => 1, 'status[!]' => mWiki::STATUS_DEL], ['id', 'channel']);
//$categoryWikiList = mWiki::selectByWhere(['id' => $categoryWikiIds, 'status[!]' => mWiki::STATUS_DEL], ['id', 'channel']);
//$byNumWikiList = mWiki::selectByWhere(['id' => $clickByNumTopWikiIds, 'status[!]' => mWiki::STATUS_DEL], ['id', 'channel']);
//foreach ($rankingWikiList as $row) {
//    if ($row['channel'] == mWiki::CHANNEL_TAOBAO) {
//        $taobaoCount++;
//    } else if ($row['channel'] == mWiki::CHANNEL_JD) {
//        $jdCount++;
//    }
//}
//foreach ($categoryWikiList as $row) {
//    if ($row['channel'] == mWiki::CHANNEL_TAOBAO) {
//        $taobaoCount++;
//    } else if ($row['channel'] == mWiki::CHANNEL_JD) {
//        $jdCount++;
//    }
//}
//
//foreach ($byNumWikiList as $row) {
//    if ($row['channel'] == mWiki::CHANNEL_TAOBAO) {
//        $taobaoCount++;
//    } else if ($row['channel'] == mWiki::CHANNEL_JD) {
//        $jdCount++;
//    }
//}
//var_dump('需要每天更新的核心商品#淘宝商品数：' . $taobaoCount . '#京东商品数:' . $jdCount);

//$where = [
//    'addtime[>]' => date('Y-m-d', strtotime('-30 days'))
//];
//$taobao30DayCount = mWiki::selectCountWhere(['addtime[>]' => date('Y-m-d', strtotime('-30 days')), 'channel' => mWiki::CHANNEL_TAOBAO]);
//var_dump(mWiki::log());
//$jd30DayCount = mWiki::selectCountWhere(['addtime[>]' => date('Y-m-d', strtotime('-30 days')), 'channel' => mWiki::CHANNEL_JD]);
//var_dump('近30天添加的淘宝商品数:' . $taobao30DayCount . '#京东商品数:' . $jd30DayCount);

$rankingWikiList = mWiki::selectByWhere(['basic_id' => $rankingBasicIds, 'status[!]' => mWiki::STATUS_DEL], ['id', 'channel']);
$rankingWikiIds = array_column($rankingWikiList, 'id');

$allWikiIds = array_merge($rankingWikiIds, $clickByNumTopWikiIds, $categoryWikiIds);
$allWikiIds = array_unique($allWikiIds);
var_dump('需要更新的wiki商品总数:' . count($allWikiIds));
//$reload_field = AdmWiki::RELOAD_WIKI_BASE;//不更新图片
$reload_field = AdmWiki::RELOAD_WIKI_TBK_BASE;//不更新图片
var_dump('开始投入更新队列#更新信息为:' . $reload_field);
foreach ($allWikiIds as $wikiId) {
    GoodsInfo::addReloadTask(['id' => $wikiId, 'reload_field' => $reload_field]);
}
echo 'done';

